<template>
  <div class="app-container">
    <!-- v-permission="['alarmSet:alarmInfo:read']"
    v-permission="['alarmSet:alarmInfo:create']"
    v-permission="['alarmSet:alarmInfo:update']"
    v-permission="['alarmSet:alarmInfo:delete']" -->
    <!-- 查询和其他操作 -->
    <el-form :inline="true" class="demo-form-inline filter">
      <div>
          <el-form-item label="场地名称">
        <el-select v-model="listQuery.fsiteNo" clearable filterable placeholder="请选择场地" @change="getFpointOptions(listQuery.fsiteNo)">
        <el-option
          v-for="item in fsiteNoBelongOptions2"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>
      </el-form-item>
           <el-form-item label="场所名称">
        <el-select v-model="listQuery.fpointNo" clearable filterable placeholder="请选择场所">
        <el-option
          v-for="item in fpointOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>
      </el-form-item>
        <el-form-item label="项目名称">
        <el-select v-model="listQuery.fairitem" clearable filterable placeholder="请选择项目名称">
        <el-option
          v-for="item in sTsAiritemOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>
      </el-form-item>
          <el-form-item label="报警日期">
        <el-date-picker
          v-model="alarmdate"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"/>
      </el-form-item>
      </div>
      <div>
      <el-form-item>
        <el-button round size="medium" style="margin-left: 10px" :style="{background:buttonColor.lookup,color:buttonColor.color}" @click="handleFilter">查找</el-button>
      <!--<el-button type="primary" icon="el-icon-edit" @click="handleCreate">添加</el-button>-->
      </el-form-item>
      </div>
      </el-form>
      <!--<el-select v-model="listQuery.fareaName" clearable filterable placeholder="请选择区" >&lt;!&ndash;@change="getSchoolOptions(listQuery.fareaName)"&ndash;&gt;
        <el-option
          v-for="item in fsiteNoBelongOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>-->


    <!-- 查询结果 -->
    <el-table :height="buttonColor.clientHeightTwo" v-loading="listLoading" :data="list" :header-cell-style="tableColor" element-loading-text="正在查询中。。。" border fit highlight-current-row>
      <el-table-column :width="buttonColor.line" :index="indexMethod" align="center" label="序" type="index"/>
      <el-table-column :formatter="dateFormat" align="center" label="报警时间" prop="fwarringdate" width="200px"/>
      <!-- <el-table-column align="center" label="区" prop="fareaName"/> -->
      <el-table-column align="center" label="场地名称" prop="fsiteName" width="160px"/>
      <el-table-column align="center" label="场所名称" prop="fpointName" width="120px"/>
      <el-table-column align="center" label="储物柜" prop="fcabname" width="120px"/>
      <el-table-column align="center" label="储物仓" prop="fboxname" width="120px"/>
      <el-table-column align="center" label="空气项目" prop="fairitemName" width="120px"/>
      <el-table-column align="center" label="报警值" prop="fcurnum" width="70px"/>
      <!--<el-table-column align="center" label="设定起始值" prop="fbegnum"/>
      <el-table-column align="center" label="设定结束值" prop="fendnum"/>-->
      <el-table-column align="left" label="报警说明" prop="fdescribe"/>
      <el-table-column align="left" label="邮件发送" prop="fsendemailStr" width="80px"/>
      <el-table-column align="left" label="短信发送" prop="fsendmsgStr" width="80px"/>

      <!--<el-table-column align="center" width="140" label="操作" class-name="small-padding">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>-->
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <!-- 添加或修改对话框 -->
    <!--<el-dialog :close-on-click-modal="false" :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="dataForm" status-icon label-position="left" label-width="110px" style="width: 400px; margin-left:50px;">
        <el-form-item label="报警时间" prop="fwarringdate">
          <el-input v-model="dataForm.fwarringdate"/>
        </el-form-item>
        <el-form-item label="区" prop="fareaName">
          <el-input v-model="dataForm.fareaName"/>
        </el-form-item>
        <el-form-item label="学校名称" prop="fsiteName">
          <el-input v-model="dataForm.fsiteName"/>
        </el-form-item>
        <el-form-item label="教室名称" prop="fpointName">
          <el-input v-model="dataForm.fpointName"/>
        </el-form-item>
        <el-form-item label="空气项目" prop="fairitemName">
          <el-input v-model="dataForm.fairitemName"/>
        </el-form-item>
        <el-form-item label="报警值" prop="fcurnum">
          <el-input v-model="dataForm.fcurnum"/>
        </el-form-item>
        <el-form-item label="设定起始值" prop="fbegnum">
          <el-input v-model="dataForm.fbegnum"/>
        </el-form-item>
        <el-form-item label="设定结束值" prop="fendnum">
          <el-input v-model="dataForm.fendnum"/>
        </el-form-item>
        <el-form-item label="报警说明" prop="fdescribe">
          <el-input v-model="dataForm.fdescribe"/>
        </el-form-item>
        <el-form-item label="邮件发送" prop="fsendemailStr">
          <el-input v-model="dataForm.fsendemailStr"/>
        </el-form-item>
        <el-form-item label="短信发送" prop="fsendmsgStr">
          <el-input v-model="dataForm.fsendmsgStr"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="createData">确定</el-button>
        <el-button v-else type="primary" @click="updateData">确定</el-button>
      </div>
    </el-dialog>-->

  </div>
</template>

<script>
import { listtcCabwarring, createtcCabwarring, updatetcCabwarring, deletetcCabwarring } from '@/api/tcCabwarring'
import { sTsAiritemOptions } from '@/api/airAlarm'
import { fsiteNoBelongOptions, fpointOptions } from '@/api/area'
import Pagination from '@/components/Pagination'
import moment from 'moment/moment'
import { tableColor,buttonColor } from '@/filters/index'

export default {
  name: 'TcCabwarring',
  components: { Pagination },
  data() {
    return {
      buttonColor:buttonColor,
      list: null,
      total: 0,
      fsiteNoBelongOptions: null,
      fsiteNoBelongOptions2: null,
      sTsAiritemOptions: null,
      fpointOptions: null,
      alarmdate: null,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        begdate: undefined,
        enddate: undefined,
        fpointNo: undefined,
        fsiteNo: undefined,
        fareaName: undefined,
        sort: 'create_time',
        order: 'desc'
      },
      dataForm: {
        id: undefined,
        fentryid: undefined,
        fwarringdate: undefined,
        fareaName: undefined,
        fsiteName: undefined,
        fpointNo: undefined,
        fpointName: undefined,
        fcabname: undefined,
        fboxname: undefined,
        fairitem: undefined,
        fairitemName: undefined,
        fcurnum: undefined,
        fbegnum: undefined,
        fendnum: undefined,
        fdescribe: undefined,
        fsendmsgStr: undefined,
        fsendemailStr: undefined
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建'
      },
      tableColor: tableColor,
      // rules: {
      //   fairitem: [
      //     { required: true, message: '报警编号不能为空', trigger: 'blur' }
      //   ],
      //   fariname: [
      //     { required: true, message: '空气项目名称不能为空', trigger: 'blur' }
      //   ],
      //   ffield: [
      //     { required: true, message: '字段名不能为空', trigger: 'blur' }
      //   ]
      // },
      permissionDialogFormVisible: false,
      systemPermissions: null,
      assignedPermissions: null,
      permissionForm: {
        roleId: undefined,
        permIds: []
      }
    }
  },
  computed: {
    fsizecom() {
      return this.visibleSubmit === 'none' ? 22 : 20
    }
  },
  created() {
    this.alarmdate = [new Date(), new Date()]
    this.getList()
    fsiteNoBelongOptions(3)
      .then(response => {
        this.fsiteNoBelongOptions = response.data.data.list
      })
    fsiteNoBelongOptions(2)
      .then(response => {
        this.fsiteNoBelongOptions2 = response.data.data.list
      })
    sTsAiritemOptions()
      .then(response => {
        this.sTsAiritemOptions = response.data.data.list
      })
  },
  methods: {
    getFpointOptions(fsiteNo) {
      if(fsiteNo){
        this.listQuery.fpointNo = undefined
          fpointOptions(fsiteNo)
          .then(response => {
            this.fpointOptions = response.data.data.list
          })
          .catch(() => {
            this.fpointOptions = []
          })
      }
    },
    formatStatus: function(row, column) {
      return row.fsiteState === 1 ? '启用' : '禁用'
    },
    formatDate: function(date) {
      var ym
      var y = date.getFullYear()
      var m = date.getMonth() + 1
      var d = date.getDate()
      if (m < 10) {
        m = '0' + m
      }
      if (d < 10) {
        d = '0' + d
      }
      ym = '' + y + m + d
      return ym
    },
    dateFormat(row, column) {
      const date = row[column.property]
      if (date === undefined) {
        return ''
      }
      return moment(date).format('YYYY-MM-DD HH:mm:ss')
    },
    getList() {
      this.listLoading = true
      this.listQuery.begdate = this.alarmdate == null ? null : this.formatDate(this.alarmdate[0])
      this.listQuery.enddate = this.alarmdate == null ? null : this.formatDate(this.alarmdate[1])
      listtcCabwarring(this.listQuery)
        .then(response => {
          this.list = response.data.data.list
          this.total = response.data.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    resetForm() {
      this.dataForm = {
        id: undefined,
        fentryid: undefined,
        fwarringdate: undefined,
        fareaName: undefined,
        fsiteName: undefined,
        fpointNo: undefined,
        fpointName: undefined,
        fcabname: undefined,
        fboxname: undefined,
        fairitem: undefined,
        fairitemName: undefined,
        fcurnum: undefined,
        fbegnum: undefined,
        fendnum: undefined,
        fdescribe: undefined,
        fsendmsgStr: undefined,
        fsendemailStr: undefined
      }
    },
    handleCreate() {
      this.resetForm()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          createtcCabwarring(this.dataForm)
            .then(response => {
              this.list.unshift(response.data.data)
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '添加空气报警信息成功'
              })
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response.data.msg
              })
            })
        }
      })
    },
    handleUpdate(row) {
      this.dataForm = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          updatetcCabwarring(this.dataForm, this.$route.query.id)
            .then(() => {
              for (const v of this.list) {
                if (v.id === this.dataForm.id) {
                  const index = this.list.indexOf(v)
                  this.list.splice(index, 1, this.dataForm)
                  break
                }
              }
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '更新管理员成功'
              })
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response.data.msg
              })
            })
        }
      })
    },
    handleDelete(row) {
      this.$confirm('是否要删除该空气报警信息', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deletetcCabwarring(row)
          .then(response => {
            this.$notify.success({
              title: '成功',
              message: '删除空气报警信息成功'
            })
            const index = this.list.indexOf(row)
            this.list.splice(index, 1)
          })
          .catch(response => {
            this.$notify.error({
              title: '失败',
              message: response.data.msg
            })
          })
      })
    },
    indexMethod(index) {
      index = index + 1
      return index
    }
  }
}
</script>
